import axios from 'axios'
import store from 'store'
import router from '@/router'
import {
    Message
} from 'element-ui'
// 创建axios实例
const http = axios.create({
    baseURL: process.env.VUE_APP_BASE_API,
    timeout: 30000 // 请求超时时间
})

// 添加request拦截器 传入参数
http.interceptors.request.use(

    config => {
        return config
    },
    error => {
        Promise.reject(error)
    }
)

// 添加respone拦截器 返回数据
// http.interceptors.response.use(
//     response => {
//         const res = response.data
//         if (res.code === 100000) {
//             // 调接口成功了的返回值
//             return res.content
//         } else {
//             const urlArr = ['/UserCenter/SearchCompareResult']
//             if (!urlArr.includes(response.config.url)) {
//                 // 轮询报错不显示
//                 Message({
//                     message: res.msg || '系统错误',
//                     type: 'error',
//                     duration: 1000
//                 })
//             }
//             if (res.code === 100011) {
//                 sessionStorage.clear()
//                 router.replace('/login')
//             } else {
//                 return Promise.reject(new Error(res.msg || 'Error'))
//             }
//         }
//         // if (!res.success) {
//         //   Message({
//         //     message: res.msg || '系统错误',
//         //     type: 'error',
//         //     duration: 1000
//         //   })
//         //   return Promise.reject(new Error(res.msg || 'Error'))
//         // } else {
//         //   return res.response
//         // }
//     },
//     error => {
//         Message({
//             message: error.response.data.msg || '系统错误',
//             type: 'error',
//             duration: 1000
//         })
//         if (error.response.data.status === 401) {
//             router.replace('/login')
//             sessionStorage.clear()
//         }
//         return Promise.reject(error)
//     }
// )
//老年食堂
http.interceptors.response.use(
    response => {
      const res = response.data
      if (res.status === 200) {
        return res.response
      } else {
        const urlArr = ['/UserCenter/SearchCompareResult']
        if (!urlArr.includes(response.config.url)) {
          // 轮询报错不显示
          Message({
            message: res.msg || '系统错误',
            type: 'error',
            duration: 1000
          })
        }
        if (res.status === 401) {
          sessionStorage.clear()
          router.replace('/login')
        } else {
          return Promise.reject(new Error(res.msg || 'Error'))
        }
      }
      // if (!res.success) {
      //   Message({
      //     message: res.msg || '系统错误',
      //     type: 'error',
      //     duration: 1000
      //   })
      //   return Promise.reject(new Error(res.msg || 'Error'))
      // } else {
      //   return res.response
      // }
    },
    error => {
      Message({
        message: error.response.data.msg || '系统错误',
        type: 'error',
        duration: 1000
      })
      if (error.response.data.status === 401) {
        router.replace('/login')
        sessionStorage.clear()
      }
      return Promise.reject(error)
    }
  )
function get(url, params = {}) {
    for (const a in params) {
        if (params[a] === null || params[a] === '') {
            delete params[a]
        }
    }
    console.log(store.state, this.$store)
    // const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiYWRtaW5zIiwianRpIjoiMSIsInVzcmlkIjoiMSIsIm9yZ2lkIjoiMSIsIm9yZ3R5cGUiOiJEcHQiLCJvcmduYW1lIjoi5p2t5bee5biC5rCR5pS_5bGAIiwiaHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS93cy8yMDA4LzA2L2lkZW50aXR5L2NsYWltcy9leHBpcmF0aW9uIjoiMjAyMC8xMi8xMCAxODowNToyOSIsIm5iZiI6MTYwNzU4MDMyOSwiZXhwIjoxNjA3NTk0NzI5LCJpc3MiOiJPbkxpbmVTYWNyaWZpY2UiLCJhdWQiOiJ3ciJ9.ts1QdHMtKX_AX9R6TgRyQI019P4GlWCYd0d52kOfQXM "
    const token = store.state.login.userInfo ? store.state.login.userInfo.token : null
    return http({
        url,
        method: 'GET',
        headers: {
            'Content-type': 'application/json;charset=UTF-8',
            version: '1.0.0',
            Authorization: `Bearer ${token} `
        },
        params
    })
}

// 封装post请求
function post(url, data = {}) {
    for (const a in data) {
        if (data[a] === null || data[a] === '') {
            delete data[a]
        }
    }
    const token = store.state.login.userInfo ? store.state.login.userInfo.token : null
    return http({
        url,
        method: 'POST',
        headers: {
            'Content-type': 'application/json;charset=UTF-8',
            version: '1.0.0',
            Authorization: `Bearer ${token} `
        },
        data
    })
}

function deleted(url, params = {}) {
    const token = store.state.login.userInfo ? store.state.login.userInfo.token : null
    return http({
        url,
        method: 'DELETE',
        headers: {
            'Content-type': 'application/json;charset=UTF-8',
            version: '1.0.0',
            Authorization: `Bearer ${token} `
        },
        params
    })
}
export default {
    get,
    post,
    deleted
}